package cn.hs.ph.dao.impl;

import cn.hs.ph.dao.GoodsDao;
import cn.hs.ph.entity.Goods;
import cn.hs.ph.util.JDBCDruidUtil;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class GoodsDaoImpl implements GoodsDao {
    @Override
    public int insert(Goods goods) {
        return 0;
    }

    @Override
    public int delete(int goodsId) {
        return 0;
    }

    @Override
    public int update(int goodsStatus,int goodsId) {
        String sql = "update goods set goodsStatus=? where goodsId=?";
        Object[] obj={goodsStatus,goodsId};
        return JDBCDruidUtil.update(sql,obj);
    }

    /*
    * pageNum: 1
    pageSize: 10
    goodsName:
    goodsStatus:
    shopName:
    startTime:
    endTime: */
    @Override
    public List<Goods> selectAll(Map<String, String> map) {
        List<Goods> list = new ArrayList<>();
        String sql = "select * from goods where 1=1 ";
        List<Object> params = new ArrayList<>();
        String goodsName = map.get("goodsName");
        if (goodsName != null && !"".equals(goodsName)) {
            sql += " and goodsName like ?";
            params.add("%" + goodsName + "%");
        }
        String shopName = map.get("shopName");
        if (shopName != null && !"".equals(shopName)) {
            sql += " and goodsShopName like ?";
            params.add("%" + shopName + "%");
        }
        String goodsStatus = map.get("goodsStatus");
        if (goodsStatus != null && !"".equals(goodsStatus)) {
            sql += " and goodsStatus=? ";
            params.add(goodsStatus);
        }
        String startTime = map.get("startTime");
        if (startTime != null && !"".equals(startTime)) {
            sql += " and addTime between ? and ?";
            params.add(startTime);
            params.add(map.get("endTime"));
        }
        String num = map.get("pageNum");
        if (num != null && !"".equals(num)) {
            sql+=" limit ?,?";
            int pageNum = Integer.parseInt(num);
            int pageSize = Integer.parseInt(map.get("pageSize"));
            params.add((pageNum - 1) * pageSize);
            params.add(pageSize);
        }
        Object[] obj = params.toArray();
        ResultSet res = JDBCDruidUtil.query(sql, obj);
        while (true) {
            try {
                if (!res.next()) break;
                Goods goods = new Goods(
                        res.getString(1),
                        res.getInt(2),
                        res.getInt(3),
                        res.getString(4),
                        res.getString(5),
                        res.getInt(6),
                        res.getInt(7),
                        res.getString(8),
                        res.getInt(9),
                        res.getString(10),
                        res.getInt(11),
                        res.getInt(12)
                        );
                list.add(goods);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }

        }
        JDBCDruidUtil.close();
        return list;
    }

    @Override
    public int selectCount(Map<String, String> map) {
        int total = 0;
        String sql = "select count(1) from goods where 1=1 ";
        List<Object> params = new ArrayList<>();
        String goodsName = map.get("goodsName");
        if (goodsName != null && !"".equals(goodsName)) {
            sql += " and goodsName like ?";
            params.add("%" + goodsName + "%");
        }
        String shopName = map.get("shopName");
        if (shopName != null && !"".equals(shopName)) {
            sql += " and goodsShopName like ?";
            params.add("%" + shopName + "%");
        }
        String goodsStatus = map.get("goodsStatus");
        if (goodsStatus != null && !"".equals(goodsStatus)) {
            sql += " and goodsStatus=? ";
            params.add(goodsStatus);
        }
        String startTime = map.get("startTime");
        if (startTime != null && !"".equals(startTime)) {
            sql += " and addTime between ? and ?";
            params.add(startTime);
            params.add(map.get("endTime"));
        }
        Object[] obj = params.toArray();
        ResultSet res = JDBCDruidUtil.query(sql, obj);
        while (true) {
            try {
                if (!res.next()) break;
                total = res.getInt(1);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }

        }
        JDBCDruidUtil.close();
        return total;
    }
}
